/*
 *
 *  MIT License
 *
 *  Copyright (c) 2017 Alibaba Group
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in all
 *  copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *  SOFTWARE.
 *
 */

package com.smarth.zhizulian.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.smarth.zhizulian.R;
import com.smarth.zhizulian.entity.UserInfo;
import com.smarth.zhizulian.utils.GlideUtils;
import com.smarth.zhizulian.utils.MoneyUtils;

/**
 * Created by mikeafc on 15/11/26.
 */
public class SharePagerAdapter extends PagerAdapter {
    private int[] backgrounds;
    private int[] colors;
    private UserInfo ui;
    private Context context;

    public SharePagerAdapter(Context context, UserInfo ui, int[] backgrounds, int[] colors) {
        this.backgrounds = backgrounds;
        this.ui = ui;
        this.context = context;
        this.colors = colors;
    }

    @Override
    public int getCount() {
        return backgrounds.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(container.getContext()).inflate(R.layout.item_shareme, null);
        ImageView iv_avatar = view.findViewById(R.id.iv_avatar);
        ImageView iv_background = view.findViewById(R.id.iv_background);
        TextView tv_money = view.findViewById(R.id.tv_money);
        TextView tv_moneyunit = view.findViewById(R.id.tv_moneyunit);
        TextView tv_name = view.findViewById(R.id.tv_name);
        ImageView iv_qrcode = view.findViewById(R.id.iv_qrcode);
        GlideUtils.loadAvatar(context, ui.getAvatar(), iv_avatar);
        tv_money.setText(MoneyUtils.coin2Money(ui.getCoin()));
        tv_name.setText(ui.getName());
        iv_background.setImageResource(backgrounds[position]);
        GlideUtils.loadNews(context, ui.getQrcode(), iv_qrcode);
        tv_money.setTextColor(context.getResources().getColor(colors[position]));
        tv_moneyunit.setTextColor(context.getResources().getColor(colors[position]));
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        LinearLayout view = (LinearLayout) object;
        container.removeView(view);
    }
}
